﻿package com.toncen.samepms.net.action;

import java.util.List;

import com.toncen.samepms.common.AbstractDatabaseAction;
import com.toncen.samepms.net.dao.ChannelDao;
import com.toncen.samepms.net.dao.InformationDao;
import com.toncen.samepms.net.domain.ChanelInformation;
import com.toncen.samepms.net.domain.Channel;
import com.toncen.samepms.net.domain.Information;
import com.sky.common.string.StringUtils;

/**
 * 描述：信息表NET_INFORMATION的action文件
 * 
 * @author xyp
 * @time 2013-5-10
 */
public class InformationAction extends AbstractDatabaseAction<Information, InformationDao> {
	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1365475264052L;

	private String channelIds;

	private String channelNames;

	public String getChannelIds() {
		return channelIds;
	}

	public void setChannelIds(String channelIds) {
		this.channelIds = channelIds;
	}

	public InformationAction() throws Exception {
		super();
	}

	/**
	 * 初始化用户
	 * 
	 * @return the sumTime
	 */
	@Override
	public void prepareOnDoNew() throws Exception {
		super.prepareOnDoNew();
		getDomain().setPublisher(getCurrentUser());
		getDomain().setCreator(getCurrentUser());
		getDomain().setAuthor(getCurrentUser().getName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#prepareOnDoEdit()
	 */
	@Override
	public void prepareOnDoEdit() throws Exception {
		super.prepareOnDoEdit();
		iniPublic();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#prepareOnDoView()
	 */
	@Override
	public void prepareOnDoView() throws Exception {
		super.prepareOnDoView();
		iniPublic();
	}

	public String getChannelNames() {
		return channelNames;
	}

	public void setChannelNames(String channelNames) {
		this.channelNames = channelNames;
	}

	/**
	 * 保存之前初始化信息列表
	 * 
	 * @return the sumTime
	 */
	@Override
	public void prepareOnDoSave() throws Exception {
		super.prepareOnDoSave();
		ChanelInformation chanelInformation;
		Channel channel;
		getDomain().getChanelInformation().clear();
		String[] channelId = this.channelIds.split(",");
		for (String id : channelId) { // 循环栏目id
			if (!StringUtils.isEmpty(id)) {
				chanelInformation = new ChanelInformation();
				chanelInformation.setInformation(getDomain());
				channel = new Channel();
				channel.setId(Long.parseLong(id));

				chanelInformation.setChanel(channel);
				getDomain().getChanelInformation().add(chanelInformation);
			}
		}

	}

	/**
	 * 初始化信息列表
	 * 
	 * @return the sumTime
	 */
	private void iniPublic() throws Exception {
		ChannelDao channelDao = new ChannelDao();
		StringBuffer channelNames = new StringBuffer();
		StringBuffer channelIds = new StringBuffer();
		List<ChanelInformation> chanelInformations = getDomain().getChanelInformation();
		Channel channel;
		for (ChanelInformation chanelInformation : chanelInformations) { // 循环栏目信息，并拼接栏目ID和栏目名称
			channel = channelDao.load(chanelInformation.getChanel());
			channelIds.append(",");
			channelIds.append(channel.getId());
			channelNames.append(",");
			channelNames.append(channel.getName());
		}
		setChannelIds(channelIds.toString().replaceFirst(",", "")); // 去除首个，
		setChannelNames(channelNames.toString().replaceFirst(",", ""));
	}
}
